local mingshi = fk.CreateSkill {
  name = "lingling__mingshi",
}

Fk:loadTranslationTable{
  ["lingling__mingshi"] = "命世",
  [":lingling__mingshi"] = "每轮各限一次，你可以将两张手牌当【杀】/【闪】/【桃】使用，然后本轮你下一次造成伤害/受到伤害/回复体力后，你摸三张牌。",

  ["#lingling__mingshi-slash"] = "命世：将两张手牌当【杀】使用，本轮下次造成伤害后摸三张牌",
  ["#lingling__mingshi-jink"] = "命世：将两张手牌当【闪】使用，本轮下次受到伤害后摸三张牌",
  ["#lingling__mingshi-peach"] = "命世：将两张手牌当【桃】使用，本轮下次回复体力后摸三张牌",

  ["$lingling__mingshi1"] = "再奏破阵乐，发兵！",
  ["$lingling__mingshi2"] = "生死有命，你奈何不了我。",
  ["$lingling__mingshi3"] = "我想，天命，不该就此作罢。",
}

mingshi:addEffect("viewas", {
  mute = true,
  pattern = "slash,jink,peach",
  prompt = function (self, player, selected_cards, selected)
    return "#lingling__mingshi-"..self.interaction.data
  end,
  interaction = function(self, player)
    local all_names = {"slash", "jink", "peach"}
    local names = player:getViewAsCardNames(mingshi.name, all_names, nil, player:getTableMark("lingling__mingshi-round"))
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2 and table.contains(player:getHandlyIds(), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards ~= 2 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = mingshi.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local name = use.card.name
    room:addTableMark(player, "lingling__mingshi-round", name)
    if name == "slash" then
      player:broadcastSkillInvoke(mingshi.name, 1)
      room:notifySkillInvoked(player, mingshi.name, "offensive")
    elseif name == "jink" then
      player:broadcastSkillInvoke(mingshi.name, 2)
      room:notifySkillInvoked(player, mingshi.name, "defensive")
    elseif name == "peach" then
      player:broadcastSkillInvoke(mingshi.name, 3)
      room:notifySkillInvoked(player, mingshi.name, "support")
    end
  end,
  after_use = function (self, player, use)
    if not player.dead then
      player.room:setPlayerMark(player, "lingling__mingshi_"..use.card.trueName.."-round", 1)
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getViewAsCardNames(mingshi.name, {"slash", "jink", "peach"}, nil, player:getTableMark("lingling__mingshi-round")) > 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and
      #player:getViewAsCardNames(mingshi.name, {"slash", "jink", "peach"}, nil, player:getTableMark("lingling__mingshi-round")) > 0
  end,
})

mingshi:addEffect(fk.Damage, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("lingling__mingshi_slash-round") > 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "lingling__mingshi_slash-round", 0)
    player:drawCards(3, mingshi.name)
  end,
})

mingshi:addEffect(fk.Damaged, {
  anim_type = "masochism",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("lingling__mingshi_jink-round") > 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "lingling__mingshi_jink-round", 0)
    player:drawCards(3, mingshi.name)
  end,
})

mingshi:addEffect(fk.HpRecover, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("lingling__mingshi_peach-round") > 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "lingling__mingshi_peach-round", 0)
    player:drawCards(3, mingshi.name)
  end,
})

return mingshi
